package com.fmjava.service_video.controller;


import com.fmjava.service_base.exception.MyException;
import com.fmjava.service_video.entity.Video;
import com.fmjava.service_video.feignClient.VideoClient;
import com.fmjava.service_video.service.VideoService;
import com.fmjava.utils.ResponseResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Objects;

/**
 * <p>
 * 作品视频 前端控制器
 * </p>
 *
 * @author fmjava
 * @since 2021-07-19
 */
@RestController
@RequestMapping("/service_video/content-video")
@Api(tags = "章节小节")
public class VideoController {
    @Autowired
    VideoService videoService;

    @Autowired
    VideoClient videoClient;
    @PostMapping("/addContentVideo")
    public ResponseResult addContentVideo(@RequestBody Video video)
    {
        videoService.save(video);
        return ResponseResult.ok();
    }

    @ApiOperation(value = "查询当前小节")
    @GetMapping("/getOneVideoInfo/{id}")
    public ResponseResult getOneVideoInfo(@ApiParam(name = "id",value = "作者id",required = true) @PathVariable("id") String id)
    {
        Video video = videoService.getById(id);
        if (video == null) {
            throw new MyException(20001,"数据不存在");
        }

        return ResponseResult.ok().data("video",video);
    }

    @ApiOperation(value = "修改每章小节")
    @PostMapping("/updateVideoInfo")
    public ResponseResult updateVideoInfo(@ApiParam(name = "video", value = "修改video基本信息",required = true)@RequestBody Video video){
        videoService.updateById(video);
        return ResponseResult.ok();
    }

    @ApiOperation(value = "删除小节")
    @PostMapping("/deleteContentVideoInfo/{id}")
    public ResponseResult deleteContentVideoInfo(@ApiParam(name = "id", value = "删除id",required = true)@PathVariable("id") String id)
    {
        //查询当前小节是否有视频信息
        Video byId = videoService.getById(id);
        String videoSourceId = byId.getVideoSourceId();
        if(!Objects.equals(null,videoSourceId)||!Objects.equals("",videoSourceId)){
            videoClient.deleteVod(videoSourceId);
        }
        videoService.removeById(id);
        return ResponseResult.ok();
    }

}

